// This is the DEVICE side. By setting the specific bit of ILAT register
// to 1 will make the core go into the interrupt. By attaching interrupt 
// handler, make the core to print out "OK"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "e_lib.h"

#include "int_types_e.h"

//char *outbuf = (char *)_BufOffset;
char outbuf[_BufSize] SECTION("shared_dram");

unsigned irq_type[8] = {E_SW_EXCEPTION, E_MEM_FAULT, E_TIMER0_INT, E_TIMER1_INT, 
                        E_MESSAGE_INT, E_DMA0_INT, E_DMA1_INT, E_USER_INT};
char* irq_name[] = {"E_SW_Exception", "E_MEM_FAULT", "E_TIMER0_INT", "E_TIMER1_INT",  
		 "E_MESSAGE_INT", "E_DMA0_INT", "E_DMA1_INT", "E_USER_INT"};

void __attribute__((interrupt)) shared_isr(int notused) {
	sprintf(outbuf + strlen(outbuf),"*");
	return;
}	

void test_irq(int i, unsigned row, unsigned col)
{
	unsigned irq = irq_type[i];
	sprintf(outbuf+strlen(outbuf), "%s(", irq_name[i]);	// print out the name of the interrupt	
	e_irq_attach(irq, shared_isr);				// Attach the ISR with this interrupt
	e_irq_mask(irq, E_FALSE);				// Clear the IMASK for this irq type
	e_irq_global_mask(E_FALSE);				// Enable the global interrupt	
	e_irq_set(row, col, irq);				// Set interrupt request bit
								// Interrupt should happen here and print '*'
	e_irq_clear(row, col, irq);				// Clear interrupt request bit
	e_irq_mask(irq, E_TRUE);				// Mask this irq type
	e_irq_global_mask(E_TRUE);				// Mask all interrupts
	sprintf(outbuf+strlen(outbuf), ") ");	                // print ')' to mark ent of this test
}

int main(void)
{
	e_coreid_t coreid = e_get_coreid();	// Who am I? Query the CoreID from hardware.
	unsigned *p = (unsigned *)0x00006000;
	int row = *pRow;
	int col = *pCol;
	int n_irqs = sizeof(irq_type)/sizeof(irq_type[0]);
	int i;	

	sprintf(outbuf, "coreid: 0x%x ", coreid);

	for (i = 0; i < n_irqs; i++) { 
		test_irq(i, row, col);
	}
	return 0;
}


